package com.tyut.controller;

import com.tyut.pojo.Items;
import com.tyut.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    @RequestMapping("/itemsAdd")
    public String itemsAdd(Model model, Items items) {
        String status = items.getStatus();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        items.setTime(df.format(new Date()));
        try {
            if("进楼".equals(status)) {
                itemsService.insertItems(items);
                return "redirect:itemsIn";
            }else {
                itemsService.insertItems(items);
                return "redirect:itemsOut";
            }
        }catch(Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "登记出现错误");
            return "failed";
        }
    }

    @RequestMapping("/itemsIn")
    public String itemsIn(Model model) {
        List<Items> itemsIn = itemsService.findItemsIn("进楼");
        model.addAttribute("list", itemsIn);
        return "itemsin";
    }

    @RequestMapping("/itemsOut")
    public String itemsOut(Model model) {
        List<Items> itemsOut = itemsService.findItemsOut("出楼");
        model.addAttribute("list", itemsOut);
        return "itemsout";
    }

    @RequestMapping("/itemsAddJSP")
    public String itemsAddJSP(){
        return "itemsadd";
    }

}
